package pageTest.login;

import constants.HostConstant;
import constants.UserInfoConstant;
import lombok.extern.slf4j.Slf4j;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import utils.WebDriverUtils;

import java.util.List;

@Slf4j
public class IndexPage extends WebDriverUtils {

    private static final String INDEX_URL = "http://" + HostConstant.HOST + ":5173/";
    public IndexPage() {
        super(INIT_URL);
    }

    @BeforeClass
    public void setUp() {
        driver.get(super.LOGIN_PAGE_URL);
        driver.findElement(By.cssSelector("#van-field-1-input")).sendKeys(UserInfoConstant.USER_ACCOUNT);
        driver.findElement(By.cssSelector("#van-field-2-input")).sendKeys(UserInfoConstant.USER_PASSWORD);
        WebElement loginButton = driver.findElement(By.cssSelector("#login > form > div:nth-child(2) > button.van-button.van-button--primary.van-button--normal.van-button--block.van-button--round"));
        Actions actions = new Actions(driver);
        // 使用动作链点击元素
        actions.moveToElement(loginButton).click().perform();
        // 确保登录成功了(登录操作响应已经返回， cookie 被种上)
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), "CAMPUS 伙伴匹配"));
    }

    @BeforeMethod
    public void beforeEachTest() {
        driver.get(INDEX_URL);
    }

    @Test
    public void matchSuccess() {
        // 点击匹配按钮不会跳转至登录页
        String topNavBarSel = "#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis";
        driver.findElement(By.className("van-switch")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector(topNavBarSel), "CAMPUS 伙伴匹配"));
    }

    @Test
    public void followSuccess() {
        // 点击关注按钮后，变为“取消关注”
        String originText = driver.findElement(By.className("van-button__text")).getText();
        String expectText = "取消关注";
        if (!originText.equals("关注")) {
            expectText = "关注";
        }
        driver.findElements(By.tagName("button")).get(0).click();
        wait.until(ExpectedConditions.textToBe(By.className("van-button__text"),expectText));
    }

    @Test
    public void chatSuccess() {
        // 1. 若存在用户，则可以查找到 “私聊” 按钮
        try {
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1)")));
        } catch (Exception e) {
            log.info("没有推荐用户");
            return;
        }

        WebElement chatButton = driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div.van-card__bottom > div.van-card__num > button:nth-child(2) > div > span"));
        assert "私聊".equals(chatButton.getText());
        String username = driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div:nth-child(1) > div.van-card__title.van-multi-ellipsis--l2")).getText();
        // 2. 点击 “私聊” 按钮，跳转至聊天室页面，且导航条为该用户名称
        driver.findElement(By.cssSelector("#van-tab-2 > div > div > div.van-list > div:nth-child(1) > div > div > div.van-card__bottom > div.van-card__num > button:nth-child(2)")).click();
        wait.until(ExpectedConditions.textToBe(By.cssSelector("#app > div:nth-child(1) > div > div > div > div.van-nav-bar__title.van-ellipsis"), username));
        // 3. 发送信息并验证消息是否发送成功
        String sendMessage = "hello world";
        driver.findElement(By.tagName("input")).sendKeys(sendMessage);
        driver.findElement(By.cssSelector("#app > div.chat-container > div.van-cell-group.van-cell-group--inset > div > div > div > div > button")).click();
        // 刷新页面，查看最后一条信息与刚刚发送的是否一致
        driver.navigate().refresh();
        List<WebElement> messageList = driver.findElements(By.className("text"));
        String lastMessage = messageList.get(messageList.size() - 1).getText();
        assert lastMessage.equals(sendMessage);
    }



    @AfterClass
    public void tearDown() {
        driver.quit();
    }
}
